<!-- 弹窗 详情 -->
<script lang="ts" setup>
import type { FormInstance, FormRules } from "element-plus";

import { whManagerBsTbSafeEnviroManagerSaveOrUpdateUsingPost } from "@/genapi/wh";
import { useAsync } from "@/hooks/use-async";
import { useCompanyStore } from "@/stores/company";
import { whManagerBsTbEnterpriseGetListUsingPost } from "@/genapi/wh";
import { computedLoading } from "@/hooks/computed-loading";
import { SAFETY_TYPE_OPTIONS } from "@/config/enum-options";

defineOptions({ name: "DetailDialog" });

const companyStore = useCompanyStore();

const dialogRef = shallowRef<InstanceType<GlobalComponents["PromiseDialog"]>>();
const formRef = shallowRef<FormInstance>();

const dialogType = ref<"add" | "edit" | "detail" | string>();

const titleMap: Record<string, string> = {
	add: "新增",
	edit: "编辑",
	detail: "详情",
};

const beforeClose = async () => {
	if (dialogType.value !== "detail") {
		await ElMessageBox.confirm("确认关闭？");
	}
};

const trigger = async (type = "add", value = {} as any) => {
	dialogType.value = type;
	model.value = JSON.parse(JSON.stringify(value)) as any;
	await dialogRef.value?.open();
};

defineExpose({
	trigger,
});

const model = ref<Record<string, any>>({});

const rules: FormRules<typeof model.value> = {
	xxx: { required: true, message: "企业名称不可为空", trigger: "blur" },
};

const { execute, isLoading } = useAsync(async () => {
	await formRef.value?.validate();
	await ElMessageBox.confirm("确认提交？", "提示", {
		type: "warning",
	});
  console.log(model.value)
  let filedata = {
    type: model.value.type,
    fileId: model.value.fileId[0].id,
    fileName:  model.value.fileId[0].name
  }
	await whManagerBsTbSafeEnviroManagerSaveOrUpdateUsingPost({
		data: {
			...filedata,
			enterpriseId: companyStore.currentCompanyId,
		},
	});
	ElMessage.success("提交成功");
	dialogRef.value?.complete();
});
const [companyList] = computedLoading(async () => {
	const { data } = await whManagerBsTbEnterpriseGetListUsingPost({ data: {} });
	return data?.map((item) => ({ label: item.name, value: item.id })) ?? [];
});
</script>

<template>
	<PromiseDialog
		ref="dialogRef"
		:title="titleMap[dialogType!]"
		:before-close="beforeClose"
		max-h="884px!"
		w="707px!"
	>
		<el-scrollbar>
			<el-form
				ref="formRef"
				:disabled="dialogType === 'detail'"
				:model="model"
				:rules="rules"
				inline
				label-position="top"
				class="gap-x-55px grid! grid-cols-2!"
			>
				<el-form-item label="所属企业" prop="enterpriseId" grid="col-span-2">
					<el-select-v2
						class="w-100%"
						:options="companyList"
						v-model="model['enterpriseId']"
						placeholder="请选择所属企业"
					/>
				</el-form-item>
				<el-form-item label="所属类型" prop="type" grid="col-span-2">
          <el-select-v2
            :options="[...SAFETY_TYPE_OPTIONS]"
            v-model="model['type']"
            clearable
            placeholder="请选择所属类型"
          />
				</el-form-item>
				<el-form-item label="附件信息" prop="附件信息">
					<FileUpload v-model="model['fileId']" class="w-100%!" />
				</el-form-item>
			</el-form>
		</el-scrollbar>
		<template v-if="dialogType !== 'detail'" #footer>
			<el-button @click="dialogRef?.close()">取消</el-button>
			<el-button type="primary" :is-loading="isLoading" @click="execute()"
				>确认</el-button
			>
		</template>
	</PromiseDialog>
</template>
